package org.example.paging;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.bean.Student;
import org.example.mapper.StudentMapper;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectPaging() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
         SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        PageHelper.startPage(1,10);

        List<Student> List = mapper.selectAll();

        List.forEach(System.out::println);

        PageInfo<Student> info = new PageInfo<>(List);
        System.out.println("总条数" + info.getTotal());
        System.out.println("总页数" + info.getPages());
        System.out.println("当前页" + info.getPageNum());
        System.out.println("每页显示条数" + info.getPageSize());
        System.out.println("上一页" + info.getPrePage());
        System.out.println("下一页" + info.getNextPage());
        System.out.println("是否是第一页" + info.isIsFirstPage());
        System.out.println("是否是最后一页" + info.isIsLastPage());

        sqlSession.close();
        is.close();
    }
}
